// 注册 表单验证事件
const nameInp = document.querySelector('.username')
const pwdInp = document.querySelector('.password')
const btn = document.querySelector('.btn1')
const btnt = document.querySelector('.btn2')

// 1. 绑定点击事件
btn.onclick = function (e) {

  
  // 2. 拿到用户输入的内容
  console.log(111)
 // 阻止默认事件  
  e.preventDefault()
  const name = nameInp.value
  const pwd = pwdInp.value

  console.log(name,pwd);

  // 3. 非空验证
  if (!name || !pwd) return alert('请完整填写表单')

  // 4. 发送请求
  const data = `username=${name}&password=${pwd}`

  const xhr = new XMLHttpRequest()

  xhr.open('POST', '../php/register.php')

  xhr.onload = function () {
    const res = JSON.parse(xhr.responseText)

    if(res.code === 0){
        return
      }
      window.location.href = '../views/login.html'
  }

  xhr.setRequestHeader('content-type', 'application/x-www-form-urlencoded')

  xhr.send(data)
}